package cn.tom.config;

import cn.tom.config.customize.CustomAuthenticationFailureHandler;
import cn.tom.config.customize.CustomAuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 酒辞
 */

@Component
public class UsernamePasswordAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
    @Resource
    CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
    @Resource
    CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        JwtLoginFilter jwtLoginFilter = new JwtLoginFilter();
        jwtLoginFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        jwtLoginFilter.setAuthenticationSuccessHandler(customAuthenticationSuccessHandler);
        jwtLoginFilter.setAuthenticationFailureHandler(customAuthenticationFailureHandler);
        http.addFilter(jwtLoginFilter);
    }
}
